package sparrowGui
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import sparrowGui.item.Item;
	import sparrowGui.skin.WindowSkin;
	import sparrowGui.utils.PubFun;

	public class Window extends Component
	{
		private var item_mini:Item;
		public function Window(argSkin:Sprite=null,title:String="")
		{
			_skin = argSkin || new WindowSkin();
			PubFun.parseObjToNew(skin,this);
			super();
			titleTxt = title;
		}
		
		override protected function init():void
		{
			addChild(skin);
			item_mini = new Item(null,skin['btn_mini'],SparrowGUI.getIns().btn_base);
//			PubFun.addChildToZW(item_mini,skin['btn_mini']);
			skin.addChild(item_mini);
			skin['btn_mini'] = item_mini;
			
			txt_title.styleSheet = SparrowGUI.txtCss;
			
			txt_title.mouseEnabled = false;
			if(btn_close) btn_close.addEventListener(MouseEvent.CLICK,onMouse);
			if(item_mini) item_mini.addEventListener(MouseEvent.CLICK,onMouse);
			titBar.addEventListener(MouseEvent.MOUSE_DOWN,onEvt);
			addEventListener(Event.ADDED_TO_STAGE,onEvt);
			addEventListener(Event.REMOVED_FROM_STAGE,onEvt);
		}
		
		// 收缩关闭事件
		private function onMouse(e:Event):void
		{
			switch(e.target){
				case btn_close:
					dispose();
					parent.removeChild(this);
					break;
				case item_mini:
					item_mini.selected = !item_mini.selected;
					if(item_mini.selected){
						win_bg.visible = false;
						panel.visible = false;
					}else{
						panel.visible = true;
						win_bg.visible = true;
					}
					break;
			}
		}
		
		// 拖动离开后台事件
		private function onEvt(e:Event):void
		{
			switch(e.type){
				case MouseEvent.MOUSE_DOWN:
					var gp:Point = this.globalToLocal(new Point(this.x,this.y));
					var rect:Rectangle = new Rectangle(gp.x,gp.y,(stage.stageWidth-titBar.height),(stage.stageHeight - 20))
					this.startDrag(false,rect);
					break;
				case MouseEvent.MOUSE_UP:
				case Event.MOUSE_LEAVE:
					this.stopDrag();
					break;
				case Event.ADDED_TO_STAGE:
					stage.addEventListener(MouseEvent.MOUSE_UP,onEvt);
					stage.addEventListener(Event.MOUSE_LEAVE,onEvt);
					break;
				case Event.REMOVED_FROM_STAGE:
					stage.removeEventListener(MouseEvent.MOUSE_UP,onEvt);
					stage.removeEventListener(Event.MOUSE_LEAVE,onEvt);
					break;
			}
		}
		
		override protected function draw():void
		{
			super.draw();
			
			var padding:int = 2;
			
			skin.width = style.width;
			skin.height = style.height;
			titBar.width = style.width;
			txt_title.width = style.width;
			txt_title.x = padding;
			btn_close.x = (style.width - padding - btn_close.width);
			btn_mini.x = (btn_close.x - btn_mini.width - padding);
			btn_mini.y = btn_close.y = padding;
			panel.y = titBar.height;
		}
		
		public function set titleTxt(val:String):void
		{
			txt_title.htmlText = PubFun.setTextTag(val,"window");
		}
		
		public function addToPanel(child:Sprite):void
		{
			if(panel) panel.addChild(child);
		}
		
		override public function dispose():void
		{
			if(btn_close) btn_close.removeEventListener(MouseEvent.CLICK,onMouse);
			if(btn_mini) btn_mini.removeEventListener(MouseEvent.CLICK,onMouse);
			titBar.removeEventListener(MouseEvent.MOUSE_DOWN,onEvt);
			removeEventListener(Event.ADDED_TO_STAGE,onEvt);
			removeEventListener(Event.REMOVED_FROM_STAGE,onEvt);
			if(stage){
				stage.removeEventListener(MouseEvent.MOUSE_UP,onEvt);
				stage.removeEventListener(Event.MOUSE_LEAVE,onEvt);
			}
			super.dispose();
		}
		
		public var titBar:Sprite;
		public var txt_title:TextField;
		public var btn_close:Sprite;
		public var btn_mini:Sprite;
		public var panel:Sprite;
		public var win_bg:Sprite;
	}
}